from util.matrix import Matrix

__author__ = 'Douglas'


class HopfieldNeuralNetwork:
    def __init__(self, number_neurons):
        self.weight_matrix = Matrix(number_neurons, number_neurons)

    def present(self, pattern):
        input_pattern = Matrix([[1 if x else -1 for x in pattern]])

        output_pattern = []

        for i in range(len(pattern)):
            column = self.weight_matrix.get_column(i)
            dot_product = (input_pattern * column)[0][0]

            output_pattern.append(True if dot_product > 0 else False)

        return output_pattern

    def train(self, pattern):
        bipolar_pattern = Matrix([pattern]).from_boolean_to_bipolar()
        m = bipolar_pattern.transpose() * bipolar_pattern
        m -= Matrix.create_identity_matrix(m.rows)
        self.weight_matrix += m